package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.EventListData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by shijunting on 15-5-25.
 */
public class CreateEventMsg extends CcMsgStructure {

    private static final String TAG = "CreateEventMsg";

    private Message mmCallback;
    private Context mContext;
    private String mEventType;
    private String mEventName;
    private String mAvatar;

    public CreateEventMsg(Message callback, Context context,String type,String name,String avatar) {
        mmCallback = callback;
        mContext = context;
        mEventType = type;
        mEventName = name;
        mAvatar = avatar;
    }

    @Override
    public void onReceive(JSONObject data) {

        EventListData eventListData = new EventListData();
        try {
            JSONObject item = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            eventListData.setCreateTime(item.getString("published_datetime"));
            eventListData.setUpdateTime(item.getString("published_datetime"));
            eventListData.setEventType(mEventType);
            eventListData.setEventName(mEventName);
            eventListData.setEventAvatar(mAvatar);
            eventListData.setHasRead("1");
            eventListData.setHasJoin("0");
            eventListData.setUserType("1");
            if (item.has("author")){
                eventListData.setAuthor(item.getString("author"));
            }
            eventListData.setUserName(item.getString("published_user_name"));
            /*if(item.has("published_user_img")){
                eventListData.setEventAvatar(item.getString("published_user_img"));
            }*/
            if (item.has("published_user_id")){
                eventListData.setUserId(item.getString("published_user_id"));
            }
            eventListData.setEventId(item.getString("event_id"));
            if(item.has("reply_count")){
                eventListData.setReplyCount(item.getString("reply_count"));
            }
            eventListData.setEventTitle(item.getString("event_title"));
            if(item.has("event_address")){
                eventListData.setActivitySite(item.getString("event_address"));
            }
            if(item.has("event_date")){
                eventListData.setActivityTime(item.getString("event_date"));
            }
            if(item.has("event_time")){
                eventListData.setActivityTime(item.getString("event_time"));
            }
            if(item.has("qrcode")){
                String qrcode = item.getString("qrcode");
                eventListData.setQrcode(qrcode);
                if (StringUtil.isEmpty(qrcode)){
                    eventListData.setIsQrcode("0");
                }else{
                    eventListData.setIsQrcode("1");
                }
            }
            if (item.has("member_count")){
                eventListData.setMemberCount(item.getString("member_count"));
            }
            if (item.has("attachment")) {
                JSONArray image = item.getJSONArray("attachment");
                StringBuilder thumbSb = new StringBuilder();
                StringBuilder oriSb = new StringBuilder();
                for (int k = 0; k < image.length(); k++) {
                    JSONObject imageObject = image.getJSONObject(k);
//                            if(imageObject.getString("type").equals("1"))
                    if (imageObject.has("thumb")) {
//                                Bitmap imageBitmap = ((BitmapDrawable) DownloadImageTask.loadImageFromNetwork(imageObject.getString("thumb"))).getBitmap();
//                                String imagePath = PhotoUtil.saveBitmaptoSdCard(imageBitmap, PhotoUtil.FOLDER_IMAGES_THUMBNAILS);
                        thumbSb.append(imageObject.getString("thumb") + ",");
                    }
                    if (imageObject.has("ori")) {
                        oriSb.append(imageObject.getString("ori") + ",");
                    }
                }
                if (thumbSb.length() > 0) {
                    eventListData.setThumbImage(thumbSb.substring(0, thumbSb.length() - 1));
                }
                if (oriSb.length() > 0) {
                    eventListData.setOriImage(oriSb.substring(0, oriSb.length() - 1));
                }
            }

            // 如果是活动
            if (mEventType.equals("7") || mEventType.equals("9")){
               eventListData.setContent1(item.getString("event_content"));
            } else if (mEventType.equals("8")){
                StringBuilder votes = new StringBuilder();
                JSONArray array = new JSONArray(item.getString("event_content"));
                for (int j = 0;j<array.length();j++){
                    String vote = array.getJSONObject(j).getString("content");
                    votes.append(vote).append(",");
                }
                eventListData.setContent1(votes.toString());
            }else{
                JSONArray eventContent = item.getJSONArray("event_content");
                if (eventContent.length() == 3){
                    eventListData.setContent1(eventContent.getJSONObject(0).getString("content"));
                    eventListData.setInitTitle1(eventContent.getJSONObject(0).getString("init_title"));

                    eventListData.setContent2(eventContent.getJSONObject(1).getString("content"));
                    eventListData.setInitTitle2(eventContent.getJSONObject(1).getString("init_title"));

                    eventListData.setContent3(eventContent.getJSONObject(2).getString("content"));
                    eventListData.setInitTitle3(eventContent.getJSONObject(2).getString("init_title"));

                }else if (eventContent.length() == 2){

                    eventListData.setContent1(eventContent.getJSONObject(0).getString("content"));
                    eventListData.setInitTitle1(eventContent.getJSONObject(0).getString("init_title"));

                    eventListData.setContent2(eventContent.getJSONObject(1).getString("content"));
                    eventListData.setInitTitle2(eventContent.getJSONObject(1).getString("init_title"));

                }else if (eventContent.length() == 1){

                    eventListData.setContent1(eventContent.getJSONObject(0).getString("content"));
                    if (eventContent.getJSONObject(0).has("init_title")){
                        eventListData.setInitTitle1(eventContent.getJSONObject(0).getString("init_title"));
                    }
                }
            }



        } catch (JSONException e) {
            e.printStackTrace();
        }


        if (mmCallback != null) {
            mmCallback.obj = eventListData;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}
